package top.cflwork.controller;

import java.util.List;
import java.util.Map;

import com.xiaoleilu.hutool.date.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import top.cflwork.vo.AdverVo;
import top.cflwork.service.AdverService;
import top.cflwork.controller.BaseController;
import top.cflwork.util.PageUtils;
import top.cflwork.util.Query;
import top.cflwork.util.R;

/**
 * 广告表
 * 
 * @author cfl
 * @email 275300091@qq.com
 * @date 2020-04-21 13:47:37
 */
 
@Controller
@RequestMapping("/adver")
public class AdverController extends BaseController{
	@Autowired
	private AdverService adverService;
	
	@GetMapping("adverPage")
	@RequiresPermissions("adver:adverPage")
	public String Adver(){
	    return "/adver/adver";
	}

	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("adver:list")
	public PageUtils list(AdverVo adverVo){
		//查询列表数据
		adverVo.setAdminId(getUserId());
		List<AdverVo> adverList = adverService.list(adverVo);
		Long total = adverService.count(adverVo);
		PageUtils pageUtils = new PageUtils(adverList, total);
		return pageUtils;
	}

    @ResponseBody
    @PostMapping("/listAll")
    @RequiresPermissions("adver:list")
    public List<AdverVo> listAll(AdverVo adverVo){
        //查询列表数据
		adverVo.setAdminId(getUserId());
        List<AdverVo> adverList = adverService.list(adverVo);
        return adverList;
    }

	@GetMapping("/add")
	@RequiresPermissions("adver:add")
	public String add(){
	    return "/adver/add";
	}

	/*@GetMapping("/edit/{id}")
	@RequiresPermissions("adver:edit")
	public String edit(@PathVariable("id") String id,Model model){
		AdverVo adver = adverService.get(id);
		model.addAttribute("adver", adver);
	    return "/adver/edit";
	}*/

    @GetMapping("/edit/{id}")
    @RequiresPermissions("adver:edit")
    @ResponseBody
    public AdverVo edit(@PathVariable("id") String id){
			AdverVo adver = adverService.get(id);
        return adver;
    }

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("adver:add")
	public R save( AdverVo adver){
		adver.setAdminId(getUserId());
		adver.setAdminName(getUsername());
		adver.setCreateTime(DateUtil.now());
		if(adverService.save(adver)>0){
			return R.ok("新增成功");
		}
		return R.error("新增失败");
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("adver:update")
	public R update( AdverVo adver){
		adverService.update(adver);
		return R.ok("修改成功");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("adver:remove")
	public R remove( String id){
		if(adverService.remove(id)>0){
		return R.ok("删除成功");
		}
		return R.error("删除失败");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("adver:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		adverService.batchRemove(ids);
		return R.ok("批量删除成功");
	}
    /**
     * 删除
     */
    @PostMapping( "/batchSave")
    @ResponseBody
    @RequiresPermissions("adver:batchSave")
    public R batchSave(List<AdverVo> adverList){
			adverService.batchSave(adverList);
        return R.ok("批量新增成功");
    }

	@ResponseBody
	@GetMapping("/listAlls")
	public List<AdverVo> listAlls(){
		List<AdverVo> adverList = adverService.listAlls();
		return adverList;
	}
}
